﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace XLN
{
    class Program
    {
        //xoa ki tu 
        public string CleanLangVN(string input)
        {
            string[] arrStr = new string[] { "thứ 2", "thứ 3", "thứ 4", "thứ 5", "thứ 6", "thứ 7", "hai", "ba", "tư", "năm", "sáu", "Sáu", "bảy", "chủ nhật", "(GMT+7)", "GMT+7", "(gmt + 7)", "ngày", "thứ", "thứ" };
            input = input.ToLower();
            foreach (string s in arrStr)
            {
                input = input.Replace(s.ToLower(), "");
            }

            char[] cEx = new char[] { ',', '-', '|' };
            foreach (char c in cEx)
            {
                input = input.Replace(c, ' ');
            }

            return input;

        }
        //xoa the
        public string CleanTag(string input)
        {
            string outPut;
            outPut = "";
            string objStringPattern = "<+[a-zA-Z0-9 =\".#/áà]+>+";
            Regex objRegex = new Regex(objStringPattern);
            string[] objStringOut = objRegex.Split(input);
            foreach (string subString in objStringOut)
            {
                outPut += subString;
            }
            return outPut;
        }
        //xoa khoang trang o giua
        public string CleanSpace(string input)
        {
            string objPattern = @"[\s]+";
            Regex objReg = new Regex(objPattern);
            string output = objReg.Replace(input, " ");
            return output;
        }
       
        //chuyen "  ","  ","  " -->mang
        public string[] ArrayComma(string input)
        {
            string objPattern = @",";
            Regex objReg = new Regex(objPattern);
            string[] objStringCut = objReg.Split(input);
            return objStringCut;
        }
        //chuyen "  "("  "("  " -->mang
        public string[] ArrayCutChar(string input)
        {
            string objPattern = @"\(";
            Regex objReg = new Regex(objPattern);
            string[] objStringCut = objReg.Split(input);
            return objStringCut;
        }
        //chuyen "  "'"  "'"  " -->mang
        public string[] ArrayCutChar1(string input)
        {
            string objPattern = @"\'";
            Regex objReg = new Regex(objPattern);
            
            string[] objStringCut = objReg.Split(input);
            return objStringCut;
        }
        //chuyen "  "-"  "-"  " -->mang
        public string[] ArrayCutChar2(string input)
        {
            string objPattern = @"\-";
            Regex objReg = new Regex(objPattern);
            string[] objStringCut = objReg.Split(input);
            return objStringCut;
        }
        //chuyen month sang so
        public int ChangeMonth(string input)
        {
            string month = input.ToLower();
            int outPut;
            outPut = 0;
            switch (month)
            {
                case "một": outPut = 1;
                    break;
                case "hai": outPut = 2;
                    break;
                case "ba": outPut = 3;
                    break;
                case "bốn": outPut = 4;
                    break;
                case "năm": outPut = 5;
                    break;
                case "sáu": outPut = 6;
                    break;
                case "bảy": outPut = 7;
                    break;
                case "tám": outPut = 8;
                    break;
                case "chín": outPut = 9;
                    break;
                case "mười": outPut = 10;
                    break;
                case "mười một": outPut = 11;
                    break;
                case "mười hai": outPut = 12;
                    break;
            }
            return outPut;
        }
        //chuyen day/month/year -->mang
        public int[] ArrayDate(string input)
        {
            string objPattern = @"/";
            Regex objReg = new Regex(objPattern);
            string[] objStringCut = objReg.Split(input);
            int[] outPut = new int[] { int.Parse(objStringCut[0].ToString()), int.Parse(objStringCut[1].ToString()), int.Parse(objStringCut[2].ToString()) };
            return outPut;
        }
        //chuyen h:m:second -->mang
        public int[] ArrayHour(string input)
        {
            string[] arrPattern = {"h:", ":","h"};
            Regex objReg = new Regex(":");
            foreach (var s in arrPattern)
            {
                if (input.Contains(s))
                {
                    objReg = new Regex(s);
                    break;
                }                 
            }
                        
            string[] objStringCut = objReg.Split(input);
            //khong co second
            int second;
            if (objStringCut.Length == 2)
            {
                second = 0;
            }
            else
            {
                second = (objStringCut[2] != null) ? int.Parse(objStringCut[2].ToString()) : 0;
            }
            int hour = (objStringCut[0] != null) ? int.Parse(objStringCut[0].ToString()) : 0;
            int minutes = (objStringCut[1] != null) ? int.Parse(objStringCut[1].ToString()) : 0;
            int[] outPut = new int[] { hour, minutes, second };
            return outPut;
        }
        //xu ly cho trang danviet.vn voi dau vao :29/10/2010 | <span>06:43
        public DateTime GetDateTimeDanViet(string input)
        {
            try
            {
                string removeLang = CleanLangVN(input);
                string removeTag = CleanTag(removeLang);
                string sTemp = CleanSpace(removeTag);

                //DateTime dt = DateTime.ParseExact(xoaTrang, "dd/MM/yyyy HH:mm", null);

                string[] arrSTemp = sTemp.Split(' ');
                int[] arrDate = ArrayDate(arrSTemp[0].ToString());
                int[] arrTime = ArrayHour(arrSTemp[1].ToString());
                //--------
                DateTime d = new DateTime(arrDate[2], arrDate[1], arrDate[0], arrTime[0], arrTime[1], arrTime[2]);
                return d;
            }
            catch (Exception ex)
            {
                return DateTime.Now;
            }
        }
        //xu ly cho trang qdnd.vn voi dau vao :QĐND - Chủ Nhật, 10/10/2010, 0:3 (GMT+7)	
        public DateTime GetDateTimeQDND(string input)
        {
            try
            {
                string[] arrDate = ArrayComma(input);
                string daymonthyear = arrDate[1].Trim().ToString();
                int[] arrDate1 = ArrayDate(daymonthyear);
                string[] arrTime = ArrayCutChar(arrDate[2].ToString());
                int[] arrTime1 = ArrayHour(arrTime[0].ToString());
                //--------
                DateTime d = new DateTime(arrDate1[2], arrDate1[1], arrDate1[0], arrTime1[0], arrTime1[1], arrTime1[2]);
                return d;
            }
            catch (Exception ex) { return DateTime.Now; }
        }
        //xu ly cho trang doanhnhan360.com
        //"11:11' AM - Thứ tư
        public DateTime GetDateDoanhNhan(string input)
        {
            try
            {

                string[] arrDate = ArrayComma(input);
                string daymonthyear = arrDate[1].Trim().ToString();

                int[] arrDate1 = ArrayDate(daymonthyear);
                
                string[] arrTime = ArrayCutChar1(arrDate[0].Trim().ToString());
                int[] arrTime1 = ArrayHour(arrTime[0].Trim().ToString());
                //--------
                DateTime d = new DateTime(arrDate1[2], arrDate1[1], arrDate1[0], arrTime1[0], arrTime1[1], arrTime1[2]);
                return d;
            }
            catch (Exception ex) { return DateTime.Now; }
        }
        //xu ly cho trang ven.vn
        public DateTime GetDateVen(string input)
        {
            try
            {
                string[] arrDate = input.Split(' ');
                DateTime d;
                if (arrDate.Length == 6)
                {
                    int day = int.Parse(arrDate[0].ToString());
                    int month = ChangeMonth(arrDate[2].ToString());
                    int year = int.Parse(arrDate[3].ToString());
                    int[] arrTime = ArrayHour(arrDate[4].Trim().ToString());
                    //--------
                    d = new DateTime(year, month, day, arrTime[0], arrTime[1], arrTime[2]);
                }
                else
                {
                    int day = int.Parse(arrDate[0].ToString());
                    string month1 = arrDate[2].ToString();
                    string month2 = arrDate[3].ToString();
                    string month12 = month1 + " " + month2;
                    int month = ChangeMonth(month12.ToString());
                    int year = int.Parse(arrDate[4].ToString());
                    int[] arrTime = ArrayHour(arrDate[5].Trim().ToString());
                    //--------
                    d = new DateTime(year, month, day, arrTime[0], arrTime[1], arrTime[2]);
                }
                return d;
            }
            catch (Exception ex) { return DateTime.Now; }
        }
        //xu ly cho trang vnrock.com
        public DateTime GetDateRockVn(string input)
        {
            try
            {
                string[] arrDate = input.Split(' ');
                int day = int.Parse(arrDate[3].ToString());
                int month = int.Parse(arrDate[2].ToString());
                int year = int.Parse(arrDate[4].ToString());
                //---------
                DateTime d = new DateTime(year, month, day);
                return d;
            }
            catch (Exception ex)
            {
                return DateTime.Now;
            }
        }
        //xu ly cho trang kenh14.vn
        public DateTime GetDateKenh14(string input)
        {
            try
            {
                string clearTag = CleanTag(input);
                string[] arrDate = clearTag.Split(' ');
                int[] arrDate1 = ArrayDate(arrDate[0].Trim().ToString());
                int[] arrTime = ArrayHour(arrDate[1].Trim().ToString());
                //---------
                DateTime d = new DateTime(arrDate1[2], arrDate1[1], arrDate1[0], arrTime[0], arrTime[1], arrTime[2]);
                return d;
            }
            catch (Exception ex)
            {
                return DateTime.Now;
            }
        }
        //xu ly cho trang khoahoc.com
        public DateTime GetDateKhoaHoc(string input)
        {
            try
            {
                string clearTag = CleanTag(input);
                string[] arrDate = clearTag.Split(' ');
                int[] arrDate1 = ArrayDate(arrDate[2].Trim().ToString());
                string[] arrTime = arrDate[0].Split('h');
                int h = int.Parse(arrTime[0].Trim().ToString());
                string m = arrTime[1].Trim().ToString();
                int m1 = int.Parse(m.Substring(1, 1).ToString());
                int second = 0;
                //---------
                DateTime d = new DateTime(arrDate1[2], arrDate1[1], arrDate1[0], h, m1, second);
                return d;
            }
            catch (Exception ex)
            {
                return DateTime.Now;
            }
        }
        //xu ly cho trang ktdt.com.vn
        public DateTime GetDateKtdt(string input)
        {
            try
            {
                string[] arrDate = input.Split(' ');
                int[] arrDate1 = ArrayDate(arrDate[2].Trim().ToString());
                string time = arrDate[0].Substring(0, 4).ToString();
                string[] arrTime = time.Split('h');
                string h = arrTime[0].ToString();
                string m = arrTime[1].ToString();
                if (h.StartsWith("0"))
                {
                    h = h.Substring(1, 1);
                }
                if (m.StartsWith("0"))
                {
                    m = m.Substring(1, 1);
                }
                int h1 = int.Parse(h.ToString());
                int m1 = int.Parse(m.ToString());
                int second1 = 0;
                //-------
                DateTime d = new DateTime(arrDate1[2], arrDate1[1], arrDate1[0], h1, m1, second1);
                return d;
            }
            catch (Exception ex)
            {
                return DateTime.Now;
            }
        }
        //xu ly cho trang bee.net.vn
        public DateTime GetDateBee(string input)
        {
            try
            {
                string cleanTag = CleanTag(input);
                string[] arrDate = cleanTag.Split(' ');
                string arrDate1 = arrDate[0].Trim().ToString();
                string arrTime = arrDate[1].Trim().ToString();
                int[] arrDate2 = ArrayDate(arrDate1);
                int[] arrTime1 = ArrayHour(arrTime);
                //-------------
                DateTime d = new DateTime(arrDate2[2], arrDate2[1], arrDate2[0], arrTime1[0], arrTime1[1], arrTime1[2]);
                return d;
            }
            catch (Exception ex)
            {
                return DateTime.Now;
            }
        }
        //xu ly cho trang bongda24h.vn
        public DateTime GetDateBongDa24h(string input)
        {
            try
            {
                string[] arr = input.Split(' ');
                int day = int.Parse(arr[1].ToString());
                int month = int.Parse(arr[3].ToString());
                int year = int.Parse(arr[5].ToString());
                string hm = arr[9].ToString();
                int[] arrTime = ArrayHour(hm);
                int second = 0;
                //--------------
                DateTime d = new DateTime(year, month, day, arrTime[0], arrTime[1], second);
                return d;
            }
            catch (Exception ex)
            {
                return DateTime.Now;

            }
        }
        //xu ly cho trang landtoday.net
        public DateTime GetDateLandToday(string input)
        {
            string[] arrDate = input.Split(' ');
            string day = arrDate[1].Trim().ToString();
            string h = arrDate[0].Trim().ToString();
            h = h.Substring(0, 5).ToString();
            int[] arrDate1 = ArrayDate(day);
            int[] arrTime = ArrayHour(h);
            int second = 0;
            //-------------
            DateTime d = new DateTime(arrDate1[2], arrDate1[1], arrDate1[0], arrTime[0], arrTime[1], second);
            return d;
        }
        //xu ly cho trang toquoc.gov.vn
        public DateTime GetDateToQuoc(string input)
        {
            try
            {
                string[] arr = ArrayCutChar2(input);
                string day = arr[1].Trim().ToString();
                int[] arrDate = ArrayDate(day);
                string hm = arr[0].Trim().ToString();
                string[] arrh = hm.Split('h');
                string[] arrm = hm.Split(':');
                int h = int.Parse(arrh[0].ToString());
                int m = int.Parse(arrm[1].Substring(0, 1).ToString());
                int second = 0;
                //----------
                DateTime d = new DateTime(arrDate[2], arrDate[1], arrDate[0], h, m, second);
                return d;
            }
            catch (Exception ex)
            {
                return DateTime.Now;
            }
        }
        /// <summary>
        /// Thu tu string bao gio cung la gio phut giay ngay thang nam
        /// </summary>
        /// <param name="sInput"></param>
        /// <returns></returns>
        public DateTime DoFilter(string sInput)
        {
            try
            {
                Regex regEx = new Regex("\\'");
                string output;
                output = regEx.Replace(sInput, "");
                //tach dd/mm/yyy va hh:mm
                string[] arrDate = output.Split(' ');

                //inset arr string to integer
                string daymonthyear = arrDate[1].Trim().ToString();
                int[] arrIntDate = ArrayDate(daymonthyear);

                //filter arr Time HH:mm
                int[] arrIntTime = ArrayHour(arrDate[0].Trim().ToString());
                //--------
                DateTime d = new DateTime(arrIntDate[2], arrIntDate[1], arrIntDate[0], arrIntTime[0], arrIntTime[1], arrIntTime[2]);
                return d;
            }
            catch (Exception ex) { return DateTime.Now; }
        
        }

    

        static void Main(string[] args)
        {
            Program objProgram = new Program();
            Console.WriteLine("thoi gian site dan viet :" + objProgram.GetDateTimeDanViet("04/11/2010 | <span>08:53"));
            Console.WriteLine("thoi gian site quan doi nhan dan :" + objProgram.GetDateTimeQDND("QĐND - Thứ Tư, 03/11/2010, 22:42 (GMT+7)"));
            Console.WriteLine("thoi gian site doanh nhan 360 :" + objProgram.GetDateDoanhNhan("11:11' AM - Thứ tư, 03/11/2010"));
            Console.WriteLine("thoi gian site ven :" + objProgram.GetDateVen("03 Tháng Mười Một 2010 8:36:19 SA"));//
            Console.WriteLine("thoi gian site rock vn :" + objProgram.GetDateRockVn("T3, Tháng 10 26 2010"));
            Console.WriteLine("thoi gian site kenh 14 :" + objProgram.GetDateKenh14("04/11/2010 <span class=\"viewdate\">00:00:05"));
            Console.WriteLine("thoi gian site khoa hoc :" + objProgram.GetDateKhoaHoc("14h02' ngày <a title=\"Các tin trong ngày 03/11/2010\" href=\"/daily/03112010/index.aspx\"><font color=\"#008800\">03/08/2010"));
            Console.WriteLine("thoi gian site kinh te do thi :" + objProgram.GetDateKtdt("14h18, ngày 02/11/2010"));
            Console.WriteLine("thoi gian site bee :" + objProgram.GetDateBee("03/11/2010 <span class=\"time\">18:33:08</span>"));
            Console.WriteLine("thoi gian site bong da 24h :" + objProgram.GetDateBongDa24h("ngày 19 tháng 11 năm 2010 cập nhật lúc 15:38"));
            Console.WriteLine("thoi gian site landtoday :" + objProgram.GetDateLandToday("17:18' 3/11/2010"));
            Console.WriteLine("thoi gian site to quoc :" + objProgram.GetDateToQuoc("0h:7' - 3/11/2010"));

            string input = @"!'%&^&*gfg\/er ge25256";
            string output;


            Regex regEx = new Regex("\\W|[0-9]");
            output = regEx.Replace(input, "");

            Console.WriteLine(input);
            Console.WriteLine(output);
            Console.WriteLine("----------");
            //to quoc
            string inputToQuoc = @"0h:7' 3/11/2010";
            Console.WriteLine(inputToQuoc + " " + objProgram.DoFilter(inputToQuoc));                        
            Console.WriteLine("----------");
            //land to day
            string inputLanToday = @"17:18' 3/11/2010";
            Console.WriteLine(inputLanToday + " " + objProgram.DoFilter(inputLanToday));                        
           
            Console.WriteLine("----------");
            //khoahoc
            string inputKhoahoc = @"14h02' 3/11/2010";

            Console.WriteLine(inputKhoahoc + " " + objProgram.DoFilter(inputKhoahoc));
            Console.WriteLine("----------");

            //khoahoc
            string inputKhoahoc2 = @"09h02' 3/11/2010";

            Console.WriteLine(inputKhoahoc2 + " " + objProgram.DoFilter(inputKhoahoc2));
            Console.WriteLine("----------");

            //doanh nhan
            string inputDoanhNhan = @"11:11' 3/11/2010";

            Console.WriteLine(inputDoanhNhan + " " + objProgram.DoFilter(inputDoanhNhan));
            Console.WriteLine("----------");

            //20h07" , ngày 25/09/2010
          
            

            Console.ReadLine();

        }
    }
}
